﻿using System;
using System.Collections.Generic;
using System.IO;
using CodeRun.Util;

namespace CodeRun.Utils.CommandLine
{
    public class CommandLineTool<TOptions> where TOptions : class, new()
    {
        public virtual TOptions Options { get; set; }
        public virtual FileInfo ExeFile { get; set; }
        public virtual DirectoryInfo WorkingDirectory { get; set; }

        public virtual ExecuteProgramResult Execute()
        {
            ExecuteProgramResult res = TryExecute();
            if (res.ExitCode != 0)
            {
                throw new Exception(String.Format("Tool {0} failed with exit code {1}. Output: {2}", ExeFile.FullName,
                                                  res.ExitCode, res.OutputLines.StringConcat("")));
            }
            return res;
        }

        public virtual ExecuteProgramResult TryExecute()
        {
            ExecuteProgramResult res = ProcessHelper.ExecuteProgramWithOutput(ExeFile.FullName,
                                                                              CommandLineArgumentsGenerator.Generate(
                                                                                  Options),
                                                                              WorkingDirectory != null
                                                                                  ? WorkingDirectory.FullName
                                                                                  : Directory.GetCurrentDirectory(),
                                                                              null);
            return res;
        }
    }
}